<?php
  header('Content-Type: application/json;charset=utf-8');
  header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
  header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // 允许请求的类型
  header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
  header('Access-Control-Allow-Headers: Content-Type,Content-Length,Accept-Encoding,X-Requested-with, Origin'); // 设置允许自定义请求头的字段

  require_once("conn.php");

  $username = $_POST['username'];
  $video_type = $_POST['video_type'];
  $video_id = $_POST['video_id'];

  // 获取用户id
  $sql = "select `user_id` from `user` where `user_username` = '$username'";
  $res = mysqli_query($conn, $sql);
  if(mysqli_num_rows($res) > 0) {
    $userRow = mysqli_fetch_assoc($res);
  }
  $user_id = $userRow["user_id"];
  // 获取commentid, islike
  $sql1 = "select comments.comment_id, expression.is_like from expression join comments on expression.comment_id = comments.comment_id  where expression.user_id = $user_id and comments.video_id = $video_id and comments.video_type = '$video_type'";
  $res1 = mysqli_query($conn, $sql1);
  if(mysqli_num_rows($res1) > 0) {
    foreach($res1 as $row) {
      $like_list[] = array(
        "commment_id" => $row["comment_id"],
        "is_like" => $row["is_like"],
      );
    }
  }
  if(!isset($like_list) || count($like_list) == 0)
    echo '{"data":[]}';
  else
    echo json_encode($like_list);
  // select comments.comment_id, expression.is_like from expression join comments on expression.comment_id = comments.comment_id  where expression.user_id = 1 and comments.video_id = 7 and comments.video_type = 'bangumi';

  // 获取评论
  // $sql = "select * from comments join user on comments.comment_user_id = user.user_id where video_type = '$video_type' and video_id = $video_id and parent_comment_id = 0";
  // $res = mysqli_query($conn, $sql);
  // if(mysqli_num_rows($res) > 0) {
  //   foreach($res as $row) {
  //     $comment_list[] = array(
  //       "comment_id"=>$row["comment_id"],
  //       "comment_username"=>$row["user_username"],
  //       "comment"=>$row["comment"],
  //       "good_count"=>$row["good_count"],
  //       "bad_count"=>$row["bad_count"],
  //       "comment_date"=>$row["date"],
  //     );
  //   }
  // }

  // if(!isset($comment_list) || count($comment_list) == 0)
  //   echo '{"data":[]}';
  // else
  //   echo json_encode($comment_list);
?>